<?php
namespace App\Providers;

use App\Providers\VersionTraits\_1_1_10_trait;
use App\Providers\VersionTraits\_1_2_10_1_trait;
use App\Providers\VersionTraits\_1_2_10_2_trait;
use App\Providers\VersionTraits\_1_2_10_3_trait;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class MysqlUpdateProvider extends ServiceProvider
{
    use _1_1_10_trait;
    use _1_2_10_1_trait;
    use _1_2_10_2_trait;
    use _1_2_10_3_trait;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }


    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        try {
            $current_version = Cache::get('__MINISNS_VERSION__');
            if(_empty_($current_version)){
                $current_version = '000 000 000 000';
            }
        }catch (\Exception $e){
            exit('<h1>Redis服务不可用或链接不上</h1><h2>'.$e->getMessage().'</h2>');
        }
        global $__MINI_GLOBAL_VERSION__;

        if($current_version != $__MINI_GLOBAL_VERSION__){
            $_system_inited = Settings::get('_system_inited');
            if($_system_inited === -1){
                $current_version = '000 000 000 000';
            }

            $this->tenants_tables();
            Cache::forget('system:remote:version:tip');

            if($current_version < '001 001 000 001'){
                $this->_v1_1_0_1();
            }

            if($current_version < '001 001 000 003'){
                $this->_v1_1_0_3();
            }

            if($current_version < '001 001 000 008'){
                $this->_v1_1_0_8();
            }

            if($current_version < '001 001 000 012'){
                $this->_v1_1_0_12();
            }

            if($current_version < '001 001 000 014'){
                $this->_v1_1_0_14();
            }

            if($current_version < '001 001 001 010'){
                $this->_v1_1_1_10();
            }

            if($current_version < '001 001 001 011'){
                $this->_v1_1_1_11();
            }

            if($current_version < '001 001 001 012'){
                $this->_v1_1_1_12();
            }

            if($current_version < '001 001 001 014'){
                $this->_v1_1_1_14();
            }

            if($current_version < '001 001 001 015'){
                $this->_v1_1_1_15();
            }

            if($current_version < '001 001 001 019'){
                $this->_v1_1_1_19();
            }

            if($current_version < '001 001 002 002'){
                $this->_v1_1_2_2();
            }

            if($current_version < '001 001 002 003'){
                $this->_v1_1_2_3();
            }

            if($current_version < '001 001 002 004'){
                $this->_v1_1_2_4();
            }

            if($current_version < '001 001 002 005'){
                $this->_v1_1_2_5();
            }


            if($current_version < '001 001 003 000'){
                $this->_v1_1_3();
            }

            if($current_version < '001 001 003 002'){
                $this->_v1_1_3_2();
            }


            if($current_version < '001 001 003 003'){
                $this->_v1_1_3_3();
            }

            if($current_version < '001 001 003 006'){
                $this->_v1_1_3_6();
            }

            if($current_version < '001 001 003 009'){
                $this->_v1_1_3_9();
            }

            if($current_version < '001 001 003 010'){
                $this->_v1_1_3_10();
            }

            if($current_version < '001 001 003 011'){
                $this->_v1_1_3_11();
            }

            if($current_version < '001 001 003 012'){
                $this->_v1_1_3_12();
            }

            if($current_version < '001 001 004 000'){
                $this->_v1_1_4();
            }

            if($current_version < '001 001 004 002'){
                $this->_v1_1_4_2();
            }

            if($current_version < '001 001 004 003'){
                $this->_v1_1_4_3();
            }

            if($current_version < '001 001 004 008'){
                $this->_v1_1_4_8();
            }

            if($current_version < '001 001 004 009'){
                $this->_v1_1_4_9();
            }

            if($current_version < '001 001 005 000'){
                $this->_v1_1_5();
            }

            if($current_version < '001 001 005 001'){
                $this->_v1_1_5_1();
            }

            if($current_version < '001 001 005 008'){
                $this->_v1_1_5_8();
            }

            if($current_version < '001 001 006 005'){
                $this->_v1_1_6_5();
            }

            if($current_version < '001 001 006 006'){
                $this->_v1_1_6_6();
            }

            if($current_version < '001 001 006 007'){
                $this->_v1_1_6_7();
            }

            if($current_version < '001 001 006 008'){
                $this->_v1_1_6_8();
            }

            if($current_version < '001 001 006 009'){
                $this->_v1_1_6_9();
            }

            if($current_version < '001 001 006 011'){
                $this->_v1_1_6_11();
            }

            if($current_version < '001 001 006 014'){
                $this->_v1_1_6_14();
            }

            if($current_version < '001 001 007 002'){
                $this->_v1_1_7_2();
            }

            if($current_version < '001 001 007 004'){
                $this->_v1_1_7_4();
            }

            if($current_version < '001 001 007 005'){
                $this->_v1_1_7_5();
            }

            if($current_version < '001 001 007 006'){
                $this->_v1_1_7_6();
            }

            if($current_version < '001 001 007 008'){
                $this->_v1_1_7_8();
            }

            if($current_version < '001 001 007 009'){
                $this->_v1_1_7_9();
            }

            if($current_version < '001 001 007 010'){
                $this->_v1_1_7_10();
            }

            if($current_version < '001 001 007 011'){
                $this->_v1_1_7_11();
            }

            if($current_version < '001 001 008 001'){
                $this->_v1_1_8_1();
            }

            if($current_version < '001 001 008 002'){
                $this->_v1_1_8_2();
            }

            if($current_version < '001 001 008 003'){
                $this->_v1_1_8_3();
            }

            if($current_version < '001 001 008 005'){
                $this->_v1_1_8_5();
            }

            if($current_version < '001 001 008 006'){
                $this->_v1_1_8_6();
            }

            if($current_version < '001 001 008 007'){
                $this->_v1_1_8_7();
            }

            if($current_version < '001 001 008 009'){
                $this->_v1_1_8_9();
            }

            if($current_version < '001 001 008 010'){
                $this->_v1_1_8_10();
            }

            if($current_version < '001 001 008 011'){
                $this->_v1_1_8_11();
            }

            if($current_version < '001 001 009 002'){
                $this->_v1_1_9_2();
            }

            if($current_version < '001 001 009 003'){
                $this->_v1_1_9_3();
            }

            if($current_version < '001 001 009 004'){
                $this->_v1_1_9_4();
            }

            if($current_version < '001 001 009 005'){
                $this->_v1_1_9_5();
            }

            if($current_version < '001 001 009 007'){
                $this->_v1_1_9_7();
            }

            if($current_version < '001 001 009 008'){
                $this->_v1_1_9_8();
            }

            if($current_version < '001 001 009 010'){
                $this->_v1_1_9_10();
            }

            if($current_version < '001 001 009 011'){
                $this->_v1_1_9_11();
            }

            if($current_version < '001 001 009 013'){
                $this->_v1_1_9_13();
            }

            if($current_version < '001 001 009 014'){
                $this->_v1_1_9_14();
            }

            if($current_version < '001 001 009 015'){
                $this->_v1_1_9_15();
            }

            if($current_version < '001 001 009 016'){
                $this->_v1_1_9_16();
            }

            if($current_version < '001 001 009 017'){
                $this->_v1_1_9_17();
            }

            if($current_version < '001 001 009 018'){
                $this->_v1_1_9_18();
            }

            if($current_version < '001 001 009 019'){
                $this->_v1_1_9_19();
            }

            if($current_version < '001 001 009 020'){
                $this->_v1_1_9_20();
            }

            if($current_version < '001 002 000 001'){
                $this->_v1_2_0_1();
            }

            if($current_version < '001 002 000 002'){
                $this->_v1_2_0_2();
            }

            if($current_version < '001 002 000 003'){
                $this->_v1_2_0_3();
            }

            if($current_version < '001 002 000 004'){
                $this->_v1_2_0_4();
            }

            if($current_version < '001 002 000 006'){
                $this->_v1_2_0_6();
            }

            if($current_version < '001 002 000 008'){
                $this->_v1_2_0_8();
            }

            if($current_version < '001 002 000 009'){
                $this->_v1_2_0_9();
            }

            if($current_version < '001 002 000 010'){
                $this->_v1_2_0_10();
            }

            if($current_version < '001 002 000 011'){
                $this->_v1_2_0_11();
            }

            if($current_version < '001 002 000 012'){
                $this->_v1_2_0_12();
            }

            if($current_version < '001 002 000 013'){
                $this->_v1_2_0_13();
            }

            if($current_version < '001 002 000 014'){
                $this->_v1_2_0_14();
            }

            if($current_version < '001 002 000 015'){
                $this->_v1_2_0_15();
            }

            if($current_version < '001 002 000 019'){
                $this->_v1_2_0_19();
            }

            if($current_version < '001 002 001 001'){
                $this->_v1_2_1_1();
            }

            if($current_version < '001 002 001 003'){
                $this->_v1_2_1_3();
            }

            if($current_version < '001 002 001 004'){
                $this->_v1_2_1_4();
            }

            if($current_version < '001 002 001 007'){
                $this->_v1_2_1_7();
            }

            if($current_version < '001 002 001 009'){
                $this->_v1_2_1_9();
            }

            if($current_version < '001 002 001 010'){
                $this->_v1_2_1_10();
            }

            if($current_version < '001 002 001 011'){
                $this->_v1_2_1_11();
            }

            if($current_version < '001 002 002 001'){
                $this->_v1_2_2_1();
            }

            if($current_version < '001 002 002 002'){
                $this->_v1_2_2_2();
            }

            if($current_version < '001 002 002 004'){
                $this->_v1_2_2_4();
            }

            if($current_version < '001 002 002 005'){
                $this->_v1_2_2_5();
            }

            if($current_version < '001 002 002 006'){
                $this->_v1_2_2_6();
            }

            if($current_version < '001 002 002 007'){
                $this->_v1_2_2_7();
            }

            if($current_version < '001 002 002 008'){
                $this->_v1_2_2_8();
            }

            if($current_version < '001 002 002 009'){
                $this->_v1_2_2_9();
            }

            if($current_version < '001 002 002 010'){
                $this->_v1_2_2_10();
            }

            if($current_version < '001 002 002 011'){
                $this->_v1_2_2_11();
            }

            if($current_version < '001 002 002 012'){
                $this->_v1_2_2_12();
            }

            if($current_version < '001 002 002 012'){
                $this->_v1_2_2_13();
            }

            if($current_version < '001 002 002 015'){
                $this->_v1_2_2_15();
            }

            if($current_version < '001 002 002 016'){
                $this->_v1_2_2_16();
            }

            if($current_version < '001 002 002 018'){
                $this->_v1_2_2_18();
            }

            if($current_version < '001 002 002 019'){
                $this->_v1_2_2_19();
            }

            if($current_version < '001 002 002 021'){
                $this->_v1_2_2_21();
            }

            if($current_version < '001 002 002 024'){
                $this->_v1_2_2_24();
            }

            if($current_version < '001 002 002 025'){
                $this->_v1_2_2_25();
            }

            if($current_version != $__MINI_GLOBAL_VERSION__){
                Cache::put('__MINISNS_VERSION__', $__MINI_GLOBAL_VERSION__);
            }
            if($_system_inited === -1){
                Settings::set('_system_inited', 1);
            }
            SiteUtils::forget_config_data();

        }
    }

    public function tenants_tables(){
        global $__MINI_GLOBAL_TENANTS_ARR__;
        foreach ($__MINI_GLOBAL_TENANTS_ARR__ as $tenant_id){
            if($tenant_id > 0){

                if(!Schema::hasTable('admin_permission_menu_'.$tenant_id)) {
                    Schema::create('admin_permission_menu_'.$tenant_id, function (Blueprint $table) {
                        $table->integer('permission_id');
                        $table->integer('menu_id');
                        $table->timestamps();
                        $table->unique(['permission_id', 'menu_id']);
                    });
                }

                if(!Schema::hasTable('admin_role_permissions_'.$tenant_id)) {
                    Schema::create('admin_role_permissions_'.$tenant_id, function (Blueprint $table) {
                        $table->integer('role_id');
                        $table->integer('permission_id');
                        $table->timestamps();
                        $table->unique(['role_id', 'permission_id']);
                    });
                    DB::insert('INSERT INTO '.'admin_role_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 2, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 3, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 4, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 5, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                }

                if(!Schema::hasTable('admin_role_users_'.$tenant_id)) {
                    Schema::create('admin_role_users_'.$tenant_id, function (Blueprint $table) {
                        $table->bigInteger('role_id')->unsigned()->index();
                        $table->bigInteger('user_id')->unsigned()->index();
                        $table->timestamps();

                        $table->unique(['role_id', 'user_id']);
                    });
                    DB::table('admin_role_users_'.$tenant_id)->insert([
                        'role_id' => 1,
                        'user_id' => 1,
                        'created_at' => '2021-01-26 03:40:34',
                        'updated_at' => '2021-01-26 03:40:34',
                    ]);
                }

                if(!Schema::hasTable('admin_role_menu_'.$tenant_id)) {
                    Schema::create('admin_role_menu_'.$tenant_id, function (Blueprint $table) {
                        $table->bigInteger('role_id')->unsigned()->index();
                        $table->bigInteger('menu_id')->unsigned()->index();
                        $table->timestamps();

                        $table->unique(['role_id', 'menu_id']);
                    });
                    DB::insert('INSERT INTO '.'admin_role_menu_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 1, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_menu_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 2, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_menu_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 3, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_menu_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 4, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);
                    DB::insert('INSERT INTO '.'admin_role_menu_'.$tenant_id.' VALUES (?, ?, ?, ?)', [1, 5, '2022-09-08 05:25:29', '2022-09-08 05:25:29']);

                }

                if(!Schema::hasTable('admin_settings_'.$tenant_id)) {
                    Schema::create('admin_settings_'.$tenant_id, function (Blueprint $table) {
                        $table->string('slug', 100);
                        $table->longText('value');
                        $table->timestamps();

                        $table->primary('slug');
                    });
                }

                if(!Schema::hasTable('admin_extensions_'.$tenant_id)) {
                    Schema::create('admin_extensions_'.$tenant_id, function (Blueprint $table) {
                        $table->increments('id')->unsigned();
                        $table->string('name', 100);
                        $table->string('version', 20)->default('');
                        $table->tinyInteger('is_enabled')->default(0);
                        $table->text('options')->nullable();
                        $table->timestamps();

                        $table->unique('name');
                    });
                }

                if(!Schema::hasTable('admin_extension_histories_'.$tenant_id)) {
                    Schema::create('admin_extension_histories_'.$tenant_id, function (Blueprint $table) {
                        $table->bigIncrements('id')->unsigned();
                        $table->string('name', 100);
                        $table->tinyInteger('type')->default(1);
                        $table->string('version', 20)->default('0');
                        $table->text('detail')->nullable();
                        $table->timestamps();

                        $table->index('name');
                    });
                }

                if(!Schema::hasTable('admin_roles_'.$tenant_id)) {
                    Schema::create('admin_roles_'.$tenant_id, function (Blueprint $table) {
                        $table->bigIncrements('id');
                        $table->string('name', 50);
                        $table->string('slug', 50)->unique();
                        $table->timestamps();
                    });
                    DB::table('admin_roles_'.$tenant_id)->insert([
                        'id' => 1,
                        'name' => '超级管理员',
                        'slug' => 'administrator',
                        'created_at' => '2021-01-26 03:40:33',
                        'updated_at' => '2022-09-08 05:25:29',
                    ]);
                }


                if(!Schema::hasTable('admin_users_'.$tenant_id)) {
                    Schema::create('admin_users_'.$tenant_id, function (Blueprint $table) {
                        $table->id();
                        $table->string('username', 120)->unique();
                        $table->string('password', 80);
                        $table->string('name', 255);
                        $table->string('avatar', 255)->nullable();
                        $table->string('remember_token', 100)->nullable();
                        $table->timestamps();
                    });
                    $user = [
                        'id' => 1,
                        'username' => 'admin',
                        'password' => '$2y$10$BXQRLfGBUrQcq4XRYx9SAe..KmMyNqa6acUWFY.viIQSFxy6Pe8se',
                        'name' => '超级管理员',
                        'remember_token' => NULL,
                        'created_at' => '2021-01-26 03:40:33',
                        'updated_at' => '2022-09-09 00:57:04',
                    ];

                    DB::table('admin_users_'.$tenant_id)->insert($user);
                }


                if(!Schema::hasTable('admin_permissions_'.$tenant_id)) {
                    Schema::create('admin_permissions_'.$tenant_id, function (Blueprint $table) {
                        $table->bigIncrements('id');
                        $table->string('name', 50);
                        $table->string('slug', 50)->unique();
                        $table->string('http_method', 255)->nullable();
                        $table->text('http_path')->nullable();
                        $table->integer('order')->default(0);
                        $table->bigInteger('parent_id')->default(0);
                        $table->timestamps();
                    });

                    DB::insert('INSERT INTO '.'admin_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [1, '开发配置相关（勿care）', 'auth-management', '', '', 1, 0, '2021-01-26 03:40:33', '2022-09-08 07:03:55']);
                    DB::insert('INSERT INTO '.'admin_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [2, 'Users', 'users', '', '/auth/users*', 2, 1, '2021-01-26 03:40:33', null]);
                    DB::insert('INSERT INTO '.'admin_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [3, 'Roles', 'roles', '', '/auth/roles*', 3, 1, '2021-01-26 03:40:33', null]);
                    DB::insert('INSERT INTO '.'admin_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [4, 'Permissions', 'permissions', '', '/auth/permissions*', 4, 1, '2021-01-26 03:40:33', null]);
                    DB::insert('INSERT INTO '.'admin_permissions_'.$tenant_id.' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [5, 'Menu', 'menu', '', '/auth/menu*', 5, 1, '2021-01-26 03:40:33', null]);

                }

            }
        }
    }


}
